package com.volunteer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.volunteer.common.R;
import com.volunteer.entity.Evaluation;
import com.volunteer.service.EvaluationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 活动评价控制器
 */
@Tag(name = "活动评价管理")
@Slf4j
@RestController
@RequestMapping("/evaluation")
@RequiredArgsConstructor
public class EvaluationController {

    private final EvaluationService evaluationService;

    /**
     * 创建评价
     */
    @Operation(summary = "创建评价")
    @PostMapping("/create")
    public R<Boolean> createEvaluation(@RequestBody @Validated Evaluation evaluation) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = evaluationService.createEvaluation(evaluation, userId);
        return R.ok(result);
    }

    /**
     * 审核评价
     */
    @Operation(summary = "审核评价")
    @PostMapping("/audit/{evaluationId}/{status}")
    @SaCheckRole("admin")
    public R<Boolean> auditEvaluation(
            @Parameter(description = "评价ID") @PathVariable Long evaluationId,
            @Parameter(description = "状态：1-通过，2-拒绝") @PathVariable Integer status,
            @Parameter(description = "备注") @RequestParam(required = false) String remark
    ) {
        Long operatorId = StpUtil.getLoginIdAsLong();
        boolean result = evaluationService.auditEvaluation(evaluationId, status, remark, operatorId);
        return R.ok(result);
    }

    /**
     * 查询活动评价列表
     */
    @Operation(summary = "查询活动评价列表")
    @GetMapping("/activity/{activityId}")
    public R<Page<Evaluation>> pageActivityEvaluations(
            @Parameter(description = "活动ID") @PathVariable Long activityId,
            @Parameter(description = "状态：0-待审核，1-已通过，2-已拒绝") @RequestParam(required = false) Integer status,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") long size
    ) {
        Page<Evaluation> page = new Page<>(current, size);
        Page<Evaluation> result = evaluationService.pageActivityEvaluations(page, activityId, status);
        return R.ok(result);
    }

    /**
     * 查询用户评价列表
     */
    @Operation(summary = "查询用户评价列表")
    @GetMapping("/user")
    public R<Page<Evaluation>> pageUserEvaluations(
            @Parameter(description = "状态：0-待审核，1-已通过，2-已拒绝") @RequestParam(required = false) Integer status,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") long size
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        Page<Evaluation> page = new Page<>(current, size);
        Page<Evaluation> result = evaluationService.pageUserEvaluations(page, userId, status);
        return R.ok(result);
    }

    /**
     * 检查用户是否评价活动
     */
    @Operation(summary = "检查用户是否评价活动")
    @GetMapping("/check/{activityId}")
    public R<Boolean> isUserEvaluated(
            @Parameter(description = "活动ID") @PathVariable Long activityId
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        boolean result = evaluationService.isUserEvaluated(activityId, userId);
        return R.ok(result);
    }

    /**
     * 获取用户评价信息
     */
    @Operation(summary = "获取用户评价信息")
    @GetMapping("/info/{activityId}")
    public R<Evaluation> getUserEvaluation(
            @Parameter(description = "活动ID") @PathVariable Long activityId
    ) {
        Long userId = StpUtil.getLoginIdAsLong();
        Evaluation evaluation = evaluationService.getUserEvaluation(activityId, userId);
        return R.ok(evaluation);
    }
} 